<template>
  <div class="app-container">

      <el-form :model="changeForm" label-width="110px" ref="rechargeForm" :rules="changeFormRules">

        <el-form-item label="原密码" prop="oldpassword">
          <el-input type="password" v-model="changeForm.oldpassword" style="width: 200px;" ></el-input>
        </el-form-item>
        <el-form-item label="新密码" prop="newpassword">
          <el-input type="password"   v-model="changeForm.newpassword"  style="width: 200px;"></el-input>
        </el-form-item>
        <el-form-item label="确认新密码" prop="password">
          <el-input  type="password" v-model="changeForm.password"  style="width: 200px;"></el-input>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer" style="text-align: center;width: 340px;margin-top: 20px;">
<!--        <el-button size="small" round @click.native="changeFormVisible = false">取消</el-button>-->
        <el-button size="small" round type="primary" @click.native="changePassword">保存</el-button>
      </div>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { updateUserPassword } from '@/api/downapi'
var crypto = require('crypto');

export default {
  name: 'Profile',
  components: {},
  data() {
    return {
      user: {},
      activeTab: 'activity',
      changeForm: {
        oldpassword  :  '',
        password:'',
        newpassword:''
      },
      changeFormRules:{
        oldpassword: [
          {required: true, message: '不能为空', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '不能为空', trigger: 'blur'}
        ],
        newpassword: [
          {required: true, message: '不能为空', trigger: 'blur'}
        ]
      },
    }
  },
  computed: {
    ...mapGetters([
      'name',
      'avatar',
      'roles'
    ])
  },
  created() {
  },
  methods: {
    changePassword () {
      let para = {
        oldpassword  : '',
        password:'',
        newpassword :'',
        useruuid:this.$store.getters.useruuid
      };
      if(this.changeForm.password === this.changeForm.newpassword){
        var md5 = crypto.createHash('md5');
        para.oldpassword = md5.update(this.changeForm.oldpassword).digest('hex');
        md5 = crypto.createHash('md5');
        para.password = md5.update(this.changeForm.password).digest('hex');
        md5 = crypto.createHash('md5');
        para.newpassword = md5.update(this.changeForm.newpassword).digest('hex')
        updateUserPassword(para).then( datas =>{
          let { message, code, data } = datas;
          if (code !== "0") {
            this.$message({
              message: message,
              type: 'error'
            });
          } else {
            this.$message({
              message:  '密码修改成功!',
              type:'success'
            });
          }
        });
      }else{
        const h = this.$createElement;
        this.$message({
          message: h('p', null, [
            h('span', null, '两次密码不一致')
          ])
        });
      }

    },
  }
}
</script>
